﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sync.IRC.MessageFilter
{
    class UserRecorder
    {
        Dictionary<int, string> idRecorder = new Dictionary<int, string>();
        Dictionary<string, int> userNameRecorder = new Dictionary<string, int>();

        int record_id_generator = 0;

        public void CleanRecorder()
        {
            idRecorder.Clear();
            userNameRecorder.Clear();
        }

        public int GetId(string userName)
        {
            if (userNameRecorder.ContainsKey(userName))
                return userNameRecorder[userName];
            int current_id = record_id_generator++;
            userNameRecorder[userName] = current_id;
            idRecorder[current_id] = userName;
            return current_id;
        }

        public string GetUserName(int id)
        {
            if (idRecorder.ContainsKey(id))
                return idRecorder[id];
            return "N/A";
        }
    }

    class UserHistory
    {
        public class Record
        {
            public Record(string userName, string message, int id) {
                this.userName = userName;
                this.id = id;
                this.message = message;
            }
            public string userName, message;
            public int id;
        }

        private List<Record> list = new List<Record>();

        public List<Record> GetHistoryList()
        {
            return list;
        }

        private bool isRecord = true;

        public bool IsRecord()
        {
            return isRecord;
        }

        public void EnableRecord()
        {
            isRecord = true;
        }

        public void DisEnableRecord()
        {
            isRecord = false;
        }

        private int capacity;
        
        public void ChangeCapacity(int newCapacity)
        {
            capacity = newCapacity;
            Check();
        }

        private void Check()
        {
            while (list.Count > capacity)
                list.RemoveAt(0);
        }

        public UserHistory()
        {
            capacity = 15;
        }

        public void AddRecord(string user,int id,string message)
        {
            if (!IsRecord())
                return;

            list.Add(new Record(user, message, id));
            Check();
        }
    }
}
